﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wowdpssim.test
{
    class testrunner
    {
        public testrunner()
        {
            addDictionaryItem(new skillA(this, "SINISTER STRIKE"), 0);
            addDictionaryItem(new skillA(this, "VIGOR"), 0);
            addDictionaryItem(new skillA(this, "KILLING SPREE"), 0);
            addDictionaryItem(new skillA(this, "SLICE AND DICE"), 0);
            addDictionaryItem(new skillA(this, "EVISCERATE"), 0);
            addDictionaryItem(new skillA(this, "ADRENALINE RUSH"), 0);
            addDictionaryItem(new skillA(this, "FAN OF KNIVES"), 0);
            addDictionaryItem(new skillA(this, "AMBUSH"), 0);
        }
        private Dictionary<skillInterface, int> testers = new Dictionary<skillInterface,int>();
        private int simulationIteration;

        public void run()
        {
            List<skillInterface> toUpdate = new List<skillInterface>();
            skillInterface doAction = null;
            int highestVal = 0;
            int retVal = 0;
            int input = 0;
            DateTime start = DateTime.Now;
            for (simulationIteration = 0; simulationIteration < 2000000; simulationIteration++)
            {
                if (testers.ContainsValue(simulationIteration))
                {
                    highestVal = 0;
                    retVal = 0;
                    toUpdate.Clear();
                    foreach (KeyValuePair<skillInterface, int> item in testers.OrderBy(key => key.Value))
                    {                        
                        if (item.Value == simulationIteration)
                        {
                            toUpdate.Add(item.Key);
                            retVal = item.Key.prePlan();
                            if (retVal > highestVal)
                            {
                                highestVal = retVal;
                                doAction = item.Key;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    input = doAction.doAction();
                    foreach (skillInterface postPlan in toUpdate)
                    {
                        postPlan.postPlan(input);
                    }
                }
            }
            DateTime end = DateTime.Now;
            TimeSpan duration = end - start;
            Console.WriteLine(duration);
        }

        public int getCurrentSimulationIteration()
        {
            return simulationIteration;
        }

        public bool addDictionaryItem(skillInterface skill, int nextIterationToEvaluate)
        {
            bool retValue = true;
            if (testers.ContainsKey(skill))
            {
                testers.Remove(skill);
            }
            if (nextIterationToEvaluate > simulationIteration)
            {
                testers.Add(skill, nextIterationToEvaluate);
            }
            else
            {
                retValue = false;
            }
            return retValue;
        }

    }
}
